﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

namespace Behavior.Resize
{
    public class ResizeAdorner : Adorner
    {
        private readonly VisualCollection _visuals;
        private ResizeThumb _thumbTop;
        private ResizeThumb _thumbBottom;
        private ResizeThumb _thumbLeft;
        private ResizeThumb _thumbRight;
        private ResizeThumb _thumbTL;
        private ResizeThumb _thumbTR;
        private ResizeThumb _thumbBL;
        private ResizeThumb _thumbBR;
        private Grid _grid;

        public ResizeAdorner(UIElement adornedElement) : base(adornedElement)
        {
            _visuals = new VisualCollection(this);
            _grid = new Grid { Margin = new Thickness(-5)};
            _grid.DataContext = AdornedElement;
            BuildThumbs();
        }

        private void BuildThumbs()
        {
            const double squareDim = 8;

            // bottom
            _thumbBottom = new ResizeThumb();
            _thumbBottom.Height = 2;
            _thumbBottom.VerticalAlignment = VerticalAlignment.Bottom;
            _thumbBottom.HorizontalAlignment = HorizontalAlignment.Stretch;
            _thumbBottom.Cursor = Cursors.SizeNS;
            _grid.Children.Add(_thumbBottom);

            // top
            _thumbTop = new ResizeThumb();
            _thumbTop.Height = 2;
            _thumbTop.VerticalAlignment = VerticalAlignment.Top;
            _thumbTop.HorizontalAlignment = HorizontalAlignment.Stretch;
            _thumbTop.Cursor = Cursors.SizeNS;
            _grid.Children.Add(_thumbTop);

            // left
            _thumbLeft = new ResizeThumb();
            _thumbLeft.Width = 2;
            _thumbLeft.VerticalAlignment = VerticalAlignment.Stretch;
            _thumbLeft.HorizontalAlignment = HorizontalAlignment.Left;
            _thumbLeft.Cursor = Cursors.SizeWE;
            _grid.Children.Add(_thumbLeft);

            // right
            _thumbRight = new ResizeThumb();
            _thumbRight.Width = 2;
            _thumbRight.VerticalAlignment = VerticalAlignment.Stretch;
            _thumbRight.HorizontalAlignment = HorizontalAlignment.Right;
            _thumbRight.Cursor = Cursors.SizeWE;
            _grid.Children.Add(_thumbRight);

            // top left
            _thumbTL = new ResizeThumb();
            _thumbTL.Width = squareDim;
            _thumbTL.Height = squareDim;
            _thumbTL.VerticalAlignment = VerticalAlignment.Top;
            _thumbTL.HorizontalAlignment = HorizontalAlignment.Left;
            _thumbTL.Margin = new Thickness(-_thumbTL.Height/2, -_thumbTL.Height/2, 0, 0);
            _thumbTL.Cursor = Cursors.SizeNWSE;
            _grid.Children.Add(_thumbTL);

            // top right
            _thumbTR = new ResizeThumb();
            _thumbTR.Width = squareDim;
            _thumbTR.Height = squareDim;
            _thumbTR.VerticalAlignment = VerticalAlignment.Top;
            _thumbTR.HorizontalAlignment = HorizontalAlignment.Right;
            _thumbTR.Margin = new Thickness(0, -_thumbTR.Height / 2, -_thumbTR.Width / 2, 0);
            _thumbTR.Cursor = Cursors.SizeNESW;
            _grid.Children.Add(_thumbTR);

            // bottom left
            _thumbBL = new ResizeThumb();
            _thumbBL.Width = squareDim;
            _thumbBL.Height = squareDim;
            _thumbBL.VerticalAlignment = VerticalAlignment.Bottom;
            _thumbBL.HorizontalAlignment = HorizontalAlignment.Left;
            _thumbBL.Margin = new Thickness(-_thumbBL.Width / 2, 0, 0, -_thumbBL.Height / 2);
            _thumbBL.Cursor = Cursors.SizeNESW;
            _grid.Children.Add(_thumbBL);

            // bottom right
            _thumbBR = new ResizeThumb();
            _thumbBR.Width = squareDim;
            _thumbBR.Height = squareDim;
            _thumbBR.VerticalAlignment = VerticalAlignment.Bottom;
            _thumbBR.HorizontalAlignment = HorizontalAlignment.Right;
            _thumbBR.Margin = new Thickness(0, 0, -_thumbBR.Width / 2, -_thumbBR.Height / 2);
            _thumbBR.Cursor = Cursors.SizeNWSE;
            _grid.Children.Add(_thumbBR);

            // adding grid to visuals
            _visuals.Add(_grid);
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            double adornedWidth = AdornedElement.DesiredSize.Width;
            double adornedHeight = AdornedElement.DesiredSize.Height;
            _grid.Arrange(new Rect(0,0, adornedWidth, adornedHeight));

            return finalSize;
        }

        protected override int VisualChildrenCount
        {
            get
            {
                return _visuals.Count;
            }
        }

        protected override Visual GetVisualChild(int index)
        {
            return _visuals[index];
        }
    }
}
